package com.zkingsoft.actions.developer;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.anotations.RemoveRequestToken;
import com.zkingsoft.anotations.SaveRequestToken;
import com.zkingsoft.model.Btn;
import com.zkingsoft.model.Function;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.StringUtils;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.BtnService;
import com.zkingsoft.services.FunctionService;

/**
 * @description 按钮管理aciton
 * @author 姜友瑶
 * @email 935090232@qq.com
 * @date 2016-06-26
 */
@Controller
@RequestMapping(value = "/developer/btn")
public class BtnController extends BaseController {

	@Resource
	private BtnService currentService;

	@Resource
	private FunctionService functionService;

	/**
	 * 
	 * @Description: 按钮分页查询
	 * @author:姜友瑶
	 * @param request
	 * @param btn
	 * @param pageVo
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, Btn btn, PaginationVO pageVo) {
		return showList(currentService, btn, pageVo);
	}

	/**
	 * 
	 * @Description: 按钮的新增或者修改方法
	 * @author:姜友瑶
	 * @param btn
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Btn btn) {
		if (btn.getId() != null) {
			return modify(currentService, btn, "按钮");
		} else {
			return add(currentService, btn, "按钮");
		}
	}

	/**
	 * 
	 * @Description: 按钮删除
	 * @author:姜友瑶
	 * @param keys
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	/**
	 * 
	 * @Description: 进入按钮编辑界面
	 * @author:姜友瑶
	 * @param id
	 * @param request
	 * @return 返回类型 String
	 * @date 2016年8月31日
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		Btn btn;
		if (id != null) {
			btn = currentService.findById(id);
			request.setAttribute("obj", btn);
		}
		return "developer/btn-form";
	}

	/**
	 * 
	 * @Description: 查询所有的按钮
	 * @author:姜友瑶
	 * @return 返回类型 AjaxResult
	 * @date 2016年8月31日
	 */
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		return findByModel(currentService, null);
	}

	/**
	 * 根据functionKey查询出功能对应的按钮
	 * 
	 * @param functionId
	 * @return
	 */
	@RequestMapping(value = "/functionBts")
	public @ResponseBody AjaxResult functionBts(Long functionId) {
		Function function = functionService.findById(functionId);
		List<Btn> btnList = new ArrayList<>();
		List<String> values = StringUtils.strToColl(function.getBtns(), ",");
		for (String btnValue : values) {
			Btn btnQuery = new Btn();
			btnQuery.setBtnValue(btnValue);
			btnList.addAll(currentService.findByModel(btnQuery));
		}
		;
		return new AjaxResult(AjaxResult.STATUS_OK, null, null, btnList, 0);
	}

}